package com.example.psysystemhomework.ui.dashboard;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.psysystemhomework.R;
import com.example.psysystemhomework.adapter.ReplyAdapter;
import com.example.psysystemhomework.databinding.FragmentDashboardBinding;
import com.example.psysystemhomework.util.NetUtil;
import com.example.psysystemhomework.util.ShareUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

public class DashboardFragment extends Fragment {

    private DashboardViewModel dashboardViewModel;
    private FragmentDashboardBinding binding;
    private ListView listReplyShow;
    private ReplyAdapter replyAdapter;
    private JSONObject jsonObject;
    private ShareUtil shareUtil;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        EventBus.getDefault().register(this);
        initView();

        return root;
    }

    private void initView() {
        listReplyShow = binding.listReplyShow;
        jsonObject = new JSONObject();
        replyAdapter = new ReplyAdapter(jsonObject, getContext());
        shareUtil = new ShareUtil(getContext());
        listReplyShow.setAdapter(replyAdapter);
        if (shareUtil.getUser() != null) {
            getAnswerData();
        } else {
            Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
        }
    }

    private void getAnswerData() {
        new Thread() {
            @Override
            public void run() {
                NetUtil netUtil = new NetUtil(getContext());
                JSONObject userAnswer = netUtil.getUserAnswer(shareUtil.getUser().getUserId());
                EventBus.getDefault().post(userAnswer);

                super.run();
            }
        }.start();

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(JSONObject s) {
        Log.i("获取到的回复信息", "onEvent: "+s.toString());
        if (s != null) {
            replyAdapter.setJson(s);
            replyAdapter.notifyDataSetChanged();
        } else {

            Toast.makeText(getContext(), "获取异常", Toast.LENGTH_SHORT).show();

        }

    }


    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}